﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ActivateYourGlutes.Data;
using ActivateYourGlutes.Infrastructure.MembershipServices;
using ActivateYourGlutes.Services.Model;
using AutoMapper;

namespace ActivateYourGlutes.Services.Implementation
{
    public class ResistanceExerciseService : MembershipProviderBase<Data.Model.ResistanceExercise>, IResistanceExerciseService
    {
        public ResistanceExerciseService(IEntityModelFactory factory, IMembershipService membershipService) : base(factory, membershipService)
        {
        }

        public List<ResistanceExercise> GetAll(Account requestingUser)
        {
            var entities =
                Repository.EagerlyLoadedEntities("ResistanceExerciseCategory", "Owner").Where(
                    r => r.Owner == null || r.Owner.AccountId == requestingUser.AccountId).ToList();
            return Mapper.Map<List<Data.Model.ResistanceExercise>,List<ResistanceExercise>>(entities);
        }

        public int TotalExercises(Account currentAccount)
        {
            return Repository.Entities.Count();
        }

        public List<ResistanceExercise> Exercises(Account requestingUser, int offset, int pageSize)
        {
            AuthorizeAccountAsAdministrator(requestingUser);
            var exercises = Repository.Entities.
                OrderByDescending(e => e.Name).
                Skip(offset).
                Take(pageSize).
                ToList();
            var mappedExercises = Mapper.Map<List<Data.Model.ResistanceExercise>, List<Services.Model.ResistanceExercise>>(exercises);
            return mappedExercises;
        }
    }
}
